package cn.hn.tx.rebot.task.job;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.hn.comm.Tools;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;
import cn.hn.tx.rebot.task.model.TaskExeLog;
import cn.hn.tx.uchblog.model.UchomeBlog;
import cn.hn.tx.uchblog.model.UchomeBlogField;
import cn.hn.tx.uchblog.service.IUchomeBlogService;
import cn.hn.tx.usermanager.model.PublishBlogUser;
import cn.hn.tx.usermanager.service.IUserManagerService;

public class TaskJob_PublishBlog extends ITaskJob {

	private IUserManagerService userManagerService;
	private IRebotBlogService rebotBlogService;
	private IUchomeBlogService uchomeBlogService;

	@Override
	void executeJob() {

		System.out.println("开始:" + getTaskName());

		// 任务执行记录日志
		TaskExeLog taskExeLog = new TaskExeLog();
		taskExeLog.setStartTime(Tools.getCurDate(null));
		taskExeLog.setTaskName(this.getTaskName());
		taskExeLog.setType(type().getType());
		String taskLogMemo = "";

		try {
			// 1.找到可以发日志的用户列表
			List<PublishBlogUser> listPublishBlogUser = userManagerService
					.queryPublishBlogUserListByType(PublishBlogUser.EN_ABLE_TRUE,PublishBlogUser.TYPE_BLOG);
			// 2.按照权限放入HashMap中
			Map<Integer, PublishBlogUser> userMap = new HashMap<Integer, PublishBlogUser>();
			int iTotal = 0;
			int iCount = 0;
			Iterator<PublishBlogUser> iteraPublishBlogUser = listPublishBlogUser
					.iterator();
			while (iteraPublishBlogUser.hasNext()) {
				PublishBlogUser pbu = iteraPublishBlogUser.next();
				iTotal += pbu.getWeight();
				for (int i = 0; i < pbu.getWeight(); i++) {
					userMap.put(iCount, pbu);
					iCount++;
				}
			}
			// 3.找出该次要发的机器发送日志列表
			List<Blog> listBlog =  rebotBlogService
					.queryListRebotWriteBlogByStatus(Blog.STATUS_UPDATE_NO, 1);

			// 4.发送日志,更新机器发送日志状态为更新
			Iterator<Blog> iteraBlog = listBlog.iterator();
			while (iteraBlog.hasNext()) {
				// 找到一个发送的用户
				long iRandomUserID = Tools.getRandomInt(0, iTotal);
				PublishBlogUser pbu = userMap.get(Integer.parseInt(String
						.valueOf(iRandomUserID)));

				// 日志参数
				Blog blog = iteraBlog.next();
				String title = blog.getTitle();
				title = Tools.splitAndFilterString(title, 255);
				String content = blog.getContent();
				int uid = pbu.getUid();
				String userName = pbu.getUserName();

				// 日志对象
				UchomeBlog uchomeBlog = new UchomeBlog();
				uchomeBlog.setBlogid(119);
				uchomeBlog.setClassid(0);
				uchomeBlog.setClick_1(Tools.getRandomInt(1, 30));
				uchomeBlog.setClick_2(Tools.getRandomInt(1, 30));
				uchomeBlog.setClick_3(Tools.getRandomInt(1, 30));
				uchomeBlog.setClick_4(Tools.getRandomInt(1, 30));
				uchomeBlog.setClick_5(Tools.getRandomInt(1, 30));
				uchomeBlog.setDateline(System.currentTimeMillis() / 1000);
				uchomeBlog.setFriend(0);
				uchomeBlog.setHot(Tools.getRandomInt(3, 10));
				uchomeBlog.setNoreply(0);
				uchomeBlog.setPassword(null);
				uchomeBlog.setPic(null);
				uchomeBlog.setPicflag(0);
				uchomeBlog.setReplynum(0);
				uchomeBlog.setSubject(title);
				uchomeBlog.setTopicid(0);
				uchomeBlog.setUid(uid);
				uchomeBlog.setUsername(userName);
				uchomeBlog.setViewnum(Tools.getRandomInt(3, 10));

				// 日志摘要
				UchomeBlogField ubf = new UchomeBlogField();
				ubf.setUid(uchomeBlog.getUid());
				ubf.setHotuser(" ");
				ubf.setMagiccall(0);
				ubf.setMagiccolor(0);
				ubf.setMagicpaper(0);
				ubf.setMessage(content);
				ubf.setPostip("127.0.0.1");
				ubf.setRelated(" ");
				ubf.setRelatedtime(0);
				ubf.setTag(" ");
				ubf.setTarget_ids(" ");

				// 发布日志
				uchomeBlogService.publishUchomeBlog(uchomeBlog, ubf);

				// 机器日志更新状态
				blog.setUpdate_time(Tools.getCurDate(null));
				blog.setStatus(Blog.STATUS_UPDATE_YES);
				blog.setBlogId(ubf.getBlogid());
				rebotBlogService.updateRebotWriteBlog(blog);

				taskLogMemo += uchomeBlog.getUsername() + "发表日志;";
			}
		} catch (Exception e) {
			// 记录异常
			getExceptionService().insertException(this.getClass().getName(),
					"executeJob()", e.toString());
			taskLogMemo = "任务执行失败.原因:" + e.toString();
		}
		taskExeLog.setMemo(taskLogMemo);
		taskExeLog.setEndTime(Tools.getCurDate(null));
		// 记录任务执行日志
		getTaskService().insertTaskExeLog(taskExeLog);

		System.out.println("结束:" + getTaskName());
	}

	public IUserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(IUserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public IRebotBlogService getRebotBlogService() {
		return rebotBlogService;
	}

	public void setRebotBlogService(IRebotBlogService rebotBlogService) {
		this.rebotBlogService = rebotBlogService;
	}

	public IUchomeBlogService getUchomeBlogService() {
		return uchomeBlogService;
	}

	public void setUchomeBlogService(IUchomeBlogService uchomeBlogService) {
		this.uchomeBlogService = uchomeBlogService;
	}

	@Override
	public TaskType type() {
		return TaskType.PublishBlog;
	}

}
